using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Modules.Abstracts;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.Interfaces.Filtron;

namespace FiltronASP.Modules
{
    public class mainPageModule : AbstractModule
    {

        /// <summary>
        /// Klasa odpowiedzialna za generowanie contentu przez modul.
        /// </summary>
        /// <param name="page">biezaca strona</param>
        /// <returns>content</returns>
        public override string GenerateContent(FiltronASP.Models.Page page)
        {
            string htmlToReturn = this.getMainPageByLanguageId(page.language_id);
            if (String.IsNullOrEmpty(htmlToReturn))
            {
                return "http://wixeurope.com";
            }   //if
            return "/" + htmlToReturn;
        }

        private string getMainPageByLanguageId(int? languageId)
        {
            if (!languageId.HasValue)
            {
                return null;
            }   //if
            if (languageId.Equals(0))
            {
                return null;
            }
            IPageDAO PageDAO = new PageDAO();
            ITemplateDAO TemplateDAO = new TemplateDAO();
            Template mainTemplate = TemplateDAO.FindByLangAndName(languageId.Value, "tpl_mainPage");
            if (mainTemplate != null)
            {
                int mainTemplateId = mainTemplate.template_id;
                IQueryable<Page> currentPage = PageDAO.FindByLanguageAndTemplateIdAndPublished(languageId.Value, mainTemplateId, true);
                if (currentPage != null)
                {
                    foreach (Page singlePage in currentPage)
                    {
                        return singlePage.page_link;
                    }
                }
            }
            return null;
        }
    }
}
